package com.bwie.auth.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.bwie.auth.feign.LoginFeignController;
import com.bwie.auth.service.AuthService;
import com.bwie.common.constants.JwtConstants;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.LoginRequest;
import com.bwie.common.domain.response.JwtResponse;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private LoginFeignController feignController;
    @Autowired
    private HttpServletRequest request;


    /**
     * 登录
     * @param loginRequest
     * @return
     */
    @Override
    public Result login(LoginRequest loginRequest) {
        //获取用户名称
        String userName = loginRequest.getUserName();
        //获取用户密码
        String userPwd = loginRequest.getUserPwd();
        //判断用户名称和密码是否存在
        if (StringUtils.isAllBlank(userName, userPwd)) {
            return Result.error("用户名和密码不存在");
        }
        //调用远程调用获取用户信息
        Result<User> name = feignController.findName(userName);
        User data = name.getData();
        //验证用户是否注册
        if (data == null) {
            return Result.error("用户还未注册 请先注册");
        }
        //验证用户名是否正确
        if (!ObjectUtil.equal(userName, data.getUserName())) {
            return Result.error("用户名错误 请重新输入");
        }
        //验证用户密码是否正确
        if (!ObjectUtil.equal(userPwd, data.getUserPwd())) {
            return Result.error("用户密码错误 请重新输入");
        }
        //创建一个map集合
        HashMap<String, Object> map = new HashMap<>();
        //获取用户唯一标识
        String userKey = UUID.randomUUID().toString().replace("-", "");
        map.put(JwtConstants.USER_KEY,userKey);
        //获取token
        String token = JwtUtils.createToken(map);
        //将token存入StringRedisTemplate中
        redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey, JSONObject.toJSONString(data),30, TimeUnit.MINUTES);
        //获取一个实体类进行返回
        JwtResponse jwtResponse = new JwtResponse();
        //token
        jwtResponse.setToken(token);
        //设置的时间
        jwtResponse.setExpriceTime("30MIN");
        //登录成功
        return Result.success(jwtResponse,"登录成功啦");
    }

    /**
     * 获取用户的信息
     * @return
     */
    @Override
    public User userInfo() {
        String token = request.getHeader("token");
        String userKey = JwtUtils.getUserKey(token);
        String userKey1 = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        return JSONObject.parseObject(userKey1,User.class);
    }
}
